package com.sudo.dao.admin.mapper;

import com.sudo.common.mapper.MyMapper;
import com.sudo.dao.admin.entity.SysAutoRole;
import com.sudo.dao.admin.model.bo.SysAutoRoleQueryBO;
import com.sudo.dao.admin.model.vo.SysAutoRoleVO;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;

import java.util.List;

public interface SysAutoRoleMapper extends MyMapper<SysAutoRole> {

    @Select("<script>" +
            " select sar.id,sar.username,sar.source,su.createTime ,sar.status, " +
            "   sr0.roleCode ,sr0.roleName,sr0.status roleStatus, " +
            "   sr1.roleCode setupRoleCode,sr1.roleName setupRoleName,sr1.status setupRoleStatus, " +
            "   sr2.roleCode expiryRoleCode,sr2.roleName expiryRoleName,sr2.status expiryRoleStatus, " +
            "   sar.expiration_time expirationTime,sar.is_expired isExpired  " +
            "   from sys_auto_role sar  " +
            "   left join sys_user su on sar.username = su.username  " +
            "   left join sys_user_role sur on sar.username = sur.userName " +
            "   left join sys_role sr0 on sur.roleCode = sr0.roleCode " +
            "   left join sys_role sr1 on sar.setup_role = sr1.roleCode  " +
            "   left join sys_role sr2 on sar.expiry_role = sr2.roleCode  " +
            "<where> " +
            "<if test=\"sysAutoRoleQueryBO.createTime != null and sysAutoRoleQueryBO.createTime.length > 1\">" +
            " and su.createTime <![CDATA[ >= ]]> #{sysAutoRoleQueryBO.createTime[0]} " +
            " and su.createTime <![CDATA[ <= ]]> #{sysAutoRoleQueryBO.createTime[1]} " +
            "</if>"+
            "<if test=\"sysAutoRoleQueryBO.expirationTime != null and sysAutoRoleQueryBO.expirationTime.length > 1\">" +
            " and sar.expiration_time <![CDATA[ >= ]]> #{sysAutoRoleQueryBO.expirationTime[0]} " +
            " and sar.expiration_time <![CDATA[ <= ]]> #{sysAutoRoleQueryBO.expirationTime[1]} " +
            "</if>"+
            "</where>" +
            " order by sar.id desc"+
            "</script>")
    List<SysAutoRoleVO> getAutoRoleInfo(@Param("sysAutoRoleQueryBO") SysAutoRoleQueryBO sysAutoRoleQueryBO);
}
